package com.hhd.pdf.component;

import com.hhd.pdf.CommonUtils;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.variable.CompositeFormula;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import org.dom4j.tree.DefaultText;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TextComponent extends AbstractPdfComponent {
    public List<Element> genElements(DefaultText node, GeneratorContext context) {
        if (CommonUtils.isEmpty(node.getText().trim())) return CommonUtils.newList();
        CompositeFormula formula = executeFormula(node.getText().trim(), context);
        Phrase phrase = new Phrase(formula.joinFormulas(), context.getGeneratorParameters().getFont());
        return CommonUtils.newList(phrase);
    }

    @Override
    public List<Element> doGenElements(GeneratorContext context) {
        throw new RuntimeException("TextComponent不支持创建多个元素");
    }

    @Override
    public String getName() {
        return "text";
    }
}
